import { _decorator, Component, instantiate, Node, NodePool, Prefab, warn } from 'cc';
import { ResManager } from './ResManager';
const { ccclass, property } = _decorator;

@ccclass('NodePoolMgr')
export class NodePoolMgr extends Component {
    public static Instance: NodePoolMgr = null;

    private nodePools: Object = null;

    protected onLoad(): void {
        if(NodePoolMgr.Instance !== null){
            this.destroy();
            return;
        }        
        NodePoolMgr.Instance = this;
    }

    public Init(): void{
        this.nodePools = {};
    }


    /**
     * 添加节点池
     * @param bundleName ab包名 
     * @param assetPath  资源路径
     * @param count 数量
     * @returns 
     */
    public AddNodePool(bundleName: string, assetPath:string, count: number = 0) {
        console.log('line 31');
        var poolName = bundleName + ":" + assetPath;
        if(this.nodePools[poolName]){
            return;
        }

        var assetPrefab = ResManager.inst.getAsset(bundleName,assetPath,Prefab) as Prefab;
        if(assetPrefab === null){
            warn(poolName + " can not find!");
            return;
        }

        var nodePool = new NodePool(poolName);
        this.nodePools[poolName] = nodePool;

        if(count <=0){
            return;
        }

        for (let i = 0; i < count; i++) {
            let item = instantiate(assetPrefab);
            nodePool.put(item);
        }
    }

    /**
     * 将使用完毕的节点放入对象池
     * @param bundleName ab包名
     * @param assetPath 资源路径
     * @param node 需要销毁的节点对象
     * @returns 
     */
    public PutNodeInPool(bundleName: string, assetPath:string, node: Node): void{
        console.log('==========PutNodeInPool=========')
        var poolName = bundleName + ":" + assetPath;
        if(!this.nodePools[poolName]){
            return;
        }

        var nodePool = this.nodePools[poolName];
        nodePool.put(node);
    }

    /**
     * 从对象池中获取节点
     * @param bundleName ab包名
     * @param assetPath 资源路径
     * @returns 
     */
    public GetNodeInPool(bundleName: string, assetPath:string) {
        console.log('==========GetNodeInPool=========')
        var poolName = bundleName + ":" + assetPath;
        if(!this.nodePools[poolName]){
            console.log('line 83');
            return;
        }

        var nodePool = this.nodePools[poolName];
        var item = nodePool.get();

        if(!item){
            var assetPrefab = ResManager.inst.getAsset(bundleName,assetPath,Prefab);
            if(assetPrefab === null){
                warn(poolName + "can not find!");
                return null;
            }
        }
        return item;
    }

    /**
     * 打印节点池信息
     * @param bundleName 
     * @param assetPath 
     * @returns 
     */
    public DebugNodePoolInfo(bundleName: string, assetPath:string): void{
        var poolName = bundleName + ":" + assetPath;
        if(!this.nodePools[poolName]){
            return;
        }

        var nodePool = this.nodePools[poolName];
        console.log(poolName + " NodePool has Object num " + nodePool.size());
    }

    /**
     * 清空节点池
     * @param bundleName 
     * @param assetPath 
     * @returns 
     */
    public ClearNodePool(bundleName: string, assetPath:string): void{
        var poolName = bundleName + ":" + assetPath;
        if(!this.nodePools[poolName]){
            return;
        }

        var nodePool = this.nodePools[poolName];
        nodePool.clearAll();
    }
}


